//
//  UserConnectionInfoViewController.m
//  takeout
//
//  Created by Hunter on 13-1-23.
//
//

#import "UserConnectionInfoViewController.h"


@interface UserConnectionInfoViewController ()

@end

@implementation UserConnectionInfoViewController
@synthesize myTableView;
@synthesize timePicker;
@synthesize timePickerView;
@synthesize arriveTimeFiled;

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加手势
    //后退
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    
    self.navigationController.navigationBarHidden = NO;
    //设置视图title
    self.title=@"送餐信息";
    //定义tableview
    myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [myTableView setDelegate:self];
    [myTableView setDataSource:self];
    
    [myTableView setBackgroundColor:[UIColor clearColor]];
    
//    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shoppingCartBg.png"]];
//    [background setFrame:CGRectMake(0, 0, 320, SSiphoneType == 0 ? 370 : 458)];
//    [self.view addSubview:background];
    
    [self.view addSubview:myTableView];
    //定义导航栏左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,27)];
    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"navLeftBtn" ofType:@"png"];
    [leftButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
    [leftButton setTitle:@"返 回" forState:UIControlStateNormal];
    leftButton.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    [leftButton addTarget:self action:@selector(backButtonPressdDelay) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //设置为左边按钮
    [[self navigationItem] setLeftBarButtonItem:leftItem];
    [self initTimePickerView];
    //设置view背景
    //    [tableviews setBackgroundColor:[UIColor clearColor]];
}

-(void)initTimePickerView{
    timePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, SSiphoneType==0?370:370+IOS5HEIGHT, 320, 252)];
    UIImageView *tiaoImageView=[[UIImageView alloc]init];
    tiaoImageView.frame = CGRectMake(0, 0, 320, 37);
    [tiaoImageView setImage:[UIImage imageNamed:@"pickerBg.png"]];
    [timePickerView addSubview:tiaoImageView];
    
    UIButton *submitButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    submitButton.frame = CGRectMake(261, 5, 48, 28);
    [submitButton setTitle:@"确定" forState:UIControlStateNormal];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"cancelBtnBg.png"] forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(pickerEndButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [timePickerView addSubview:submitButton];
    
    UIButton *cancelButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    cancelButton.frame = CGRectMake(7, 5, 48, 28);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"cancelBtnBg.png"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(pickerCancelButtonPress:) forControlEvents:UIControlEventTouchUpInside];
    [timePickerView addSubview:cancelButton];
    
    //初始化时间选择器
    timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 37, 320, 215)];
    timePicker.datePickerMode = UIDatePickerModeTime;
    [timePickerView addSubview:timePicker];
    
    [self.view addSubview:timePickerView];
}

//点击完成触发：
-(IBAction)pickerEndButtonPress:(id)sender
{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    [self hiddenPickerView];
}

//点击取消触发
-(IBAction)pickerCancelButtonPress:(id)sender
{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    [self hiddenPickerView];
}

//手势
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    //后退
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)backButtonPressdDelay{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    
    [self performSelector:@selector(backButtonPressd) withObject:NULL afterDelay:0.1];
}

- (void)backButtonPressd{
    //将当前视图推出栈，程序将进入上一视图
    [self.navigationController popViewControllerAnimated:YES];
}

//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableview heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

//设置section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

//设置为1个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //隐藏tableview滚动条
    tableView.showsVerticalScrollIndicator=NO;
    return 1;
}


//设置每个cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"TypeIdentifier";
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if(indexPath.row == 0){
        //添加说明
        NSString *tipMsg = @"联系人：";
        NSString *tipText = @"请输入您的名字";
        UITextView *explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 8, 80, 30)];
        explainTextView.text=tipMsg;
        explainTextView.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
        [explainTextView setTextColor:[UIColor blackColor]];
        explainTextView.editable = NO;
        explainTextView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:explainTextView];
        
        //添加输入姓名的文本框
        UIPlaceHolderTextView *nameFiled = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(75, 8, 220, 30)];
        //把换行键改成完成键
        nameFiled.returnKeyType = UIReturnKeyDone;
        nameFiled.keyboardType = UIKeyboardTypeDefault;
        nameFiled.textColor = [UIColor blackColor];
        nameFiled.font = [UIFont fontWithName:@"Arial" size:14.0];
        nameFiled.backgroundColor = [UIColor clearColor];
        nameFiled.delegate = self;
        nameFiled.scrollEnabled = YES;
        nameFiled.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [nameFiled setPlaceholder:tipText];
        [nameFiled setPlaceholderColor:[UIColor lightGrayColor]];
        
        [cell.contentView addSubview:nameFiled];
    }
    else if(indexPath.row == 1){
        //添加说明
        NSString *tipMsg = @"地址：";
        NSString *tipText = @"请尽量详细的输入您的地址";
        UITextView *explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 8, 80, 30)];
        explainTextView.text=tipMsg;
        explainTextView.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
        [explainTextView setTextColor:[UIColor blackColor]];
        explainTextView.editable = NO;
        explainTextView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:explainTextView];
        
        //添加地址的文本框
        UIPlaceHolderTextView *addressFiled = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(60, 8, 220, 30)];
        //把换行键改成完成键
        addressFiled.returnKeyType = UIReturnKeyDone;
        addressFiled.keyboardType = UIKeyboardTypeDefault;
        addressFiled.textColor = [UIColor blackColor];
        addressFiled.font = [UIFont fontWithName:@"Arial" size:14.0];
        addressFiled.backgroundColor = [UIColor clearColor];
        addressFiled.delegate = self;
        addressFiled.scrollEnabled = YES;
        addressFiled.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [addressFiled setPlaceholder:tipText];
        [addressFiled setPlaceholderColor:[UIColor lightGrayColor]];
        
        [cell.contentView addSubview:addressFiled];
    }
    else if(indexPath.row == 2){
        //添加说明
        NSString *tipMsg = @"电话：";
        NSString *tipText = @"请输入您的联系电话";
        UITextView *explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 8, 80, 30)];
        explainTextView.text=tipMsg;
        explainTextView.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
        [explainTextView setTextColor:[UIColor blackColor]];
        explainTextView.editable = NO;
        explainTextView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:explainTextView];
        
        //添加电话的文本框
        UIPlaceHolderTextView *phoneNumberFiled = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(60, 8, 220, 30)];
        //把换行键改成完成键
        phoneNumberFiled.returnKeyType = UIReturnKeyDone;
        phoneNumberFiled.keyboardType = UIKeyboardTypeDefault;
        phoneNumberFiled.textColor = [UIColor blackColor];
        phoneNumberFiled.font = [UIFont fontWithName:@"Arial" size:14.0];
        phoneNumberFiled.backgroundColor = [UIColor clearColor];
        phoneNumberFiled.delegate = self;
        phoneNumberFiled.scrollEnabled = YES;
        phoneNumberFiled.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [phoneNumberFiled setPlaceholder:tipText];
        [phoneNumberFiled setPlaceholderColor:[UIColor lightGrayColor]];
        
        [cell.contentView addSubview:phoneNumberFiled];
    }
    else if(indexPath.row == 3){
        //添加说明
        NSString *tipMsg = @"希望送达时间：";
        NSString *tipText = @"仅限当日";
        UITextView *explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 8, 120, 30)];
        explainTextView.text=tipMsg;
        explainTextView.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
        [explainTextView setTextColor:[UIColor blackColor]];
        explainTextView.editable = NO;
        explainTextView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:explainTextView];
        
        //添加希望送达时间的文本框
        arriveTimeFiled = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(115, 8, 220, 30)];
        //把换行键改成完成键
        arriveTimeFiled.returnKeyType = UIReturnKeyDone;
        arriveTimeFiled.keyboardType = UIKeyboardTypeDefault;
        arriveTimeFiled.textColor = [UIColor blackColor];
        arriveTimeFiled.font = [UIFont fontWithName:@"Arial" size:14.0];
        arriveTimeFiled.backgroundColor = [UIColor clearColor];
        arriveTimeFiled.delegate = self;
        arriveTimeFiled.scrollEnabled = YES;
        arriveTimeFiled.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        [arriveTimeFiled setPlaceholder:tipText];
        [arriveTimeFiled setPlaceholderColor:[UIColor lightGrayColor]];
        
        UIButton *selectTimeBtn = [[UIButton alloc] initWithFrame:CGRectMake(115, 8, 220, 30)];
        [selectTimeBtn setBackgroundImage:nil forState:UIControlStateNormal];
        [selectTimeBtn addTarget:self action:@selector(showPickerView) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.contentView addSubview:arriveTimeFiled];
        [cell.contentView addSubview:selectTimeBtn];
    }
    else if(indexPath.row == 4){
        //添加说明
        NSString *tipMsg = @"备注：";
        NSString *tipText = @"有什么特殊说明都可以写在这哦";
        UITextView *explainTextView = [[UITextView alloc] initWithFrame:CGRectMake(5, 8, 80, 30)];
        explainTextView.text=tipMsg;
        explainTextView.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
        [explainTextView setTextColor:[UIColor blackColor]];
        explainTextView.editable = NO;
        explainTextView.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:explainTextView];
        
        //添加备注的文本框
        UIPlaceHolderTextView *descFiled = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(60, 8, 220, 30)];
        //把换行键改成完成键
        descFiled.returnKeyType = UIReturnKeyDone;
        descFiled.keyboardType = UIKeyboardTypeDefault;
        descFiled.textColor = [UIColor blackColor];
        descFiled.font = [UIFont fontWithName:@"Arial" size:14.0];
        descFiled.backgroundColor = [UIColor clearColor];
        descFiled.delegate = self;
        descFiled.scrollEnabled = YES;
        descFiled.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        descFiled.tag = 1;
        [descFiled setPlaceholder:tipText];
        [descFiled setPlaceholderColor:[UIColor lightGrayColor]];
        
        [cell.contentView addSubview:descFiled];
    }
    //设置cell点击无反应
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


//选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

//设置section的footer的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}

//设置section的footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if(section == 1){
//        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
//        //分享给朋友按钮
//        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [shareBtn setFrame:CGRectMake(17, 10, 130, 35)];
//        NSString *filePath=[[NSBundle mainBundle] pathForResource:@"buttonBg02" ofType:@"png"];
//        [shareBtn setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
//        [shareBtn setTitle:@"短信分享" forState:UIControlStateNormal];
//        [shareBtn addTarget:self action:@selector(shareByMsg:) forControlEvents:UIControlEventTouchUpInside];
//        shareBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
//        //信息报错按钮
//        UIButton *errorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [errorBtn setFrame:CGRectMake(175, 10, 130, 35)];
//        [errorBtn setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
//        [errorBtn setTitle:@"信息报错" forState:UIControlStateNormal];
//        [errorBtn addTarget:self action:@selector(showErrorReportView:) forControlEvents:UIControlEventTouchUpInside];
//        errorBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
//        [footerView addSubview:shareBtn];
//        [footerView addSubview:errorBtn];
//        return footerView;
//    }
    return nil;
}

-(void)showPickerView{
    //开始动画
    [UIView beginAnimations: @"Animation" context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:0.3];
    //生成选取器，并向上拉动
    if(SSiphoneType==0){
        timePickerView.frame = CGRectMake(0, 215, 320, 252);
    }
    if(SSiphoneType==1){
        timePickerView.frame = CGRectMake(0, 215+IOS5HEIGHT, 320, 252);
    }
    [self slideFrameUp];
    //动画结束
    [UIView commitAnimations];
}

-(void)hiddenPickerView {
    //开始动画
    [UIView beginAnimations: @"Animation" context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:0.3];
    //生成选取器，并向上拉动
    if(SSiphoneType==0){
        timePickerView.frame = CGRectMake(0, 370, 320, 252);
    }
    if(SSiphoneType==1){
        timePickerView.frame = CGRectMake(0, 370+IOS5HEIGHT, 320, 252);
    }
    [self slideFrameDown];
    //动画结束
    [UIView commitAnimations];
}

//完成键调用的方法
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(IBAction) slideFrameUp;
{
    [self slideFrame:YES];
}

-(IBAction) slideFrameDown;
{
    [self slideFrame:NO];
}

-(void) slideFrame:(BOOL) up
{
    const int movementDistance = 100; // 垂直偏移量
    const float movementDuration = 0.3f; // 动画时间
    
    int movement = (up ? -movementDistance : movementDistance);
    
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}

//textView开始编辑时，将页面向上偏移
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if(textView.tag == 1){
        [self slideFrameUp];
    }
}

//textView开始编辑时，将页面向下偏移
- (void)textViewDidEndEditing:(UITextView *)textView{
    if(textView.tag == 1){
        [self slideFrameDown];
    }
}

- (void)viewDidUnload {
    self.myTableView = nil;
    self.timePicker = nil;
    self.timePickerView = nil;
    self.arriveTimeFiled = nil;
    [super viewDidUnload];
}

@end
